package com.bricker.laboratorymanagement.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bricker.laboratorymanagement.business.entity.Laboratory;
import com.bricker.laboratorymanagement.business.model.LabSelectModel;
import com.bricker.laboratorymanagement.business.service.LaboratoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/lab")
public class LaboratoryController {
    @Autowired
    private LaboratoryService laboratoryService;

    /**
     * 分页多条件查询实验小组
     * @param labSelectModel
     * @return
     */
    @GetMapping
    public Page<Laboratory> getLabByPage(LabSelectModel labSelectModel){
        return laboratoryService.getLabByPage(labSelectModel);
    }

    @PostMapping
    public void saveOrUpdateLab(HttpServletRequest request, @RequestBody String laboratories){
        String token=request.getHeader("access_token");
        Long uId= Long.valueOf(JWT.decode(token).getAudience().get(0));
        List<Laboratory> list = JSONObject.parseArray(laboratories,Laboratory.class);
        laboratoryService.saveOrUpdateLab(uId,list);
    }

    @DeleteMapping
    public void deleteLab(HttpServletRequest request, @RequestBody String ids){
        String token=request.getHeader("access_token");
        Long uId= Long.valueOf(JWT.decode(token).getAudience().get(0));
        List<Long> list = JSONObject.parseArray(ids,Long.class);
        laboratoryService.deleteLab(uId,list);
    }
}
